package com.xr.blog.controller.front;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xr.blog.exception.BlogException;
import com.xr.blog.pojo.BlogArticle;
import com.xr.blog.pojo.BlogComment;
import com.xr.blog.service.BlogLinkService;
import com.xr.blog.service.BlogTagService;
import com.xr.blog.tools.camera.CameraUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Controller
public class IndexController extends BaseController{
    @Autowired
    private BlogLinkService linkService;

    @Autowired
    private BlogTagService tagService;

    /**
     * 首页
     * @return
     */
    @GetMapping("/")
    public String index(Model model) {
        return index(model,1);
    }

    /**
     * 首页分页
     * @param pageIndex 需要加载的页码
     * @return
     */
    @GetMapping("/page/{pageIndex}")
    public String index(Model model,@PathVariable(value = "pageIndex") Integer pageIndex) {
        if(pageIndex==1){
            List<BlogArticle> topArticles = articleService.findAllTopArticles();
            model.addAttribute("top",topArticles);
        }
        IPage<BlogArticle> page=articleService.findPageByKeyword(new Page<>(pageIndex,5),null);
        model.addAttribute("page",page);
        // 热门文章
        List<BlogArticle> hotArticles=articleService.findHotArticles(8);
        model.addAttribute("hotArticles", hotArticles);
        // 热门标签
        model.addAttribute("hotTags", tagService.findHotTags(20));
        // 友情链接
        model.addAttribute("friendLinks", linkService.findLinkByType(1));
        // 个人链接
        model.addAttribute("personalLinks", linkService.findLinkByType(2));
        //分页代码
        model.addAttribute("pageHtml",pagination((int)page.getCurrent(),(int)page.getPages(), "/page/"));
        return "index";
    }

    /**
     * 文章详情页
     * @return
     */
    @GetMapping("{url}.html")
    public String index(Model model, @PathVariable(value = "url") String url) {
        BlogArticle info=articleService.findArticleByUrl(url);
        if(null==info){
            throw new BlogException(40004,"文章不存在："+url);
        }
        model.addAttribute("info",info);
        //查询当前文章的第一页评论
        if(info.getIsComment()){
            IPage<BlogComment> commentPage=commentService.findPageByArticleId(new Page<>(1,10),info.getId());
            model.addAttribute("comments",commentPage);
        }
        // 热门文章
        List<BlogArticle> hotArticles=articleService.findHotArticles(8);
        model.addAttribute("hotArticles", hotArticles);
        // 热门标签
        model.addAttribute("hotTags", tagService.findHotTags(20));
        // 友情链接
        model.addAttribute("friendLinks", linkService.findLinkByType(1));
        // 个人链接
        model.addAttribute("personalLinks", linkService.findLinkByType(2));
        //上一篇和下一篇
        BlogArticle previousArticle=articleService.findPreviousById(info.getId());
        model.addAttribute("previousArticle",previousArticle);
        BlogArticle nextArticle=articleService.findNextById(info.getId());
        model.addAttribute("nextArticle",nextArticle);
        //自定义文章
        if(info.getType()==1){
            return "custom";
        }
        return "article";
    }
}
